﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Graghics
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

		public int partition(int[] arr, int p, int q) //快排，排序区间为p到q（物理位置）
		{
			int lo = p;
			int hi = q;
			int temp = arr[p];//基准
			while (lo < hi)
			{
				while (hi > lo && arr[hi] >= temp) //从右往左，找到一个比基准小的数
				{
					hi--;
				}
				arr[lo] = arr[hi];
				while (hi > lo && arr[lo] <= temp) //从左往右，找到一个比基准大的数
				{
					lo++;
				}
				arr[hi] = arr[lo];
			}
			arr[hi] = temp;
			return lo;//arr[lo]的位置已经定好
		}
		public void quickSort(int[] arr, int p, int q)
		{
			int par;
			//如果待排序序列不存在，或者仅包含 1 个元素，则不再进行分割
			if (q - p <= 0)
			{
				return;
			}
			else
			{
				//调用 partition() 函数，分割 [p,q] 区域
				par = partition(arr, p, q);
				//以 [p,par-1]作为新的待排序序列，继续分割
				quickSort(arr, p, par - 1);
				//以[par+1,q]作为新的待排序序列，继续分割
				quickSort(arr, par + 1, q);
			}
		}

		public void random(int n,int m,int[] arr)//随机生成n个和为m的非负整数
        {
            //随机生成n-1个小于等于且大于等于0的数，排序，最前面补个0，最后面补个m，这n+1个数相邻的差共n个数，和为m
            Random r = new Random();
            int[] temp = new int[n + 1];
            temp[0] = 0;
            temp[n] = m;
            for(int i = 1; i < n ; i++)
            {
                temp[i] = r.Next(0, m);
            }
			quickSort(temp, 1, n - 1);
			for(int i = 0; i < n; i++)
            {
				arr[i] = temp[i + 1] - temp[i];
            }
        } 
        private void onButtonClicked(object sender, MouseEventArgs e)
        {
			const int maxLength = 20;//边长，长宽，半径的上界
            int[] arr = new int[4];//随机创建的正方形、矩形、圆形、三角形个数
            random(4, 10, arr);//随机生成4个和为10的非负整数
			finalDisplay.Text += ("创建了" + arr[0] + "个正方形" + "\r\n");
			finalDisplay.Text += ("创建了" + arr[1] + "个矩形" + "\r\n");
			finalDisplay.Text += ("创建了" + arr[2] + "个圆形" + "\r\n");
			finalDisplay.Text += ("创建了" + arr[3] + "个三角形" + "\r\n");
			Random r = new Random();
			double totalSpace = 0;
            try
            {
				for(int i = 0; i < arr[0]; i++)
                {
					Square square = new Square();
					square.op_side = r.NextDouble() * maxLength;
					finalDisplay.Text += ("第" + (i + 1) + "个正方形" + "的面积为" + square.op_space + "\r\n");
					totalSpace += square.op_space;
				}
                for (int i = 0; i < arr[1]; i++)
                {
					Rectangle rectangle = new Rectangle();
					rectangle.op_length = r.NextDouble() * maxLength;
					rectangle.op_width = r.NextDouble() * maxLength;
					finalDisplay.Text += ("第" + (i + 1) + "个矩形" + "的面积为" + rectangle.op_space + "\r\n");
                    totalSpace += rectangle.op_space;
                }
                for (int i = 0; i < arr[2]; i++)
                {
					Circular circular = new Circular(r.NextDouble() * maxLength); 
                    finalDisplay.Text += ("第" + (i + 1) + "个圆形" + "的面积为" + circular.op_space + "\r\n");
                    totalSpace += circular.op_space;
                }
                for (int i = 0; i < arr[3]; i++)
                {
                    double one = r.NextDouble() * maxLength;
                    double two = r.NextDouble() * maxLength;
					//这样会出现triangleError
					//double three = r.NextDouble() * maxLength;
					double three = r.NextDouble() * ((one > two ? two : one) * 2)
						+ (System.Math.Abs(one - two));
					Triangle triangle = new Triangle(one, two, three);

					finalDisplay.Text += ("第" + (i + 1) + "个三角形" + "的面积为" + triangle.op_space + "\r\n");
                    totalSpace += triangle.op_space;
                }
                finalDisplay.Text += ("这10个随机图形的总面积为：" + totalSpace + "\r\n");
            }
			catch(Exception a)
            {
				MessageBox.Show(a.Message);
            }
			
		}
    }
}
